package com.mzw.huaxiarealestatebackend.service.impl;
import cn.dev33.satoken.secure.SaSecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mzw.huaxiarealestatebackend.common.utils.DateFormat;
import com.mzw.huaxiarealestatebackend.dao.StaffDao;
import com.mzw.huaxiarealestatebackend.dao.UserDao;
import com.mzw.huaxiarealestatebackend.entity.*;
import com.mzw.huaxiarealestatebackend.service.StaffService;
import com.mzw.huaxiarealestatebackend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

/**
 * (Staff)表服务实现类
 *
 * @author makejava
 * @since 2023-12-11 16:21:55
 */
@Service
//("staffService")
public class StaffServiceImpl implements StaffService {
    @Resource
    private StaffDao staffDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserService userService;

    @Value("${mzw.password-key}")
    private String passwordKey;
    /**
     * 查询所有
     * @return
     */
    @Override
    public List<Staff> queryAll() {
        List<Staff> staff = staffDao.selectList(null);
        return staff;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param stfNo 主键
     * @return 实例对象
     */
    @Override
    public Staff queryById(String stfNo) {
        QueryWrapper<Staff> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stf_no", stfNo);
        Staff staff = staffDao.selectOne(queryWrapper);
        return staff;
    }

    /**
     * 模糊查询（根据职工的部分信息进行查询)
     * @param staff
     * @return
     */
    @Override
    public List<Staff> queryByCondition(Staff staff) {
        QueryWrapper<Staff> wrapper = new QueryWrapper<>();

        // 设置模糊查询条件
        if (staff.getStfNo() != null && !staff.getStfNo().isEmpty()) {
            wrapper.like("stf_no", staff.getStfNo());
        }

        if (staff.getDpNo() != null && !staff.getDpNo().isEmpty()) {
            wrapper.eq("dp_no", staff.getDpNo());
        }

        if (staff.getStfStatus() != null && !staff.getStfStatus().isEmpty()) {
            wrapper.eq("stf_status", staff.getStfStatus());
        }

        if (staff.getStfSex() != null && !staff.getStfSex().isEmpty()){
            wrapper.eq("stf_sex", staff.getStfSex());
        }

        if (staff.getStfName() != null && !staff.getStfName().isEmpty()) {
            wrapper.like("stf_name", staff.getStfName());
        }

        if (staff.getStfTel() != null && !staff.getStfTel().isEmpty()) {
            wrapper.like("stf_tel", staff.getStfTel());
        }

        if (staff.getStfQq() != null && !staff.getStfQq().isEmpty()) {
            wrapper.like("stf_qq", staff.getStfQq());
        }

        if (staff.getStfEmail() != null && !staff.getStfEmail().isEmpty()) {
            wrapper.like("stf_email", staff.getStfEmail());
        }

        if (staff.getStfLevel() != null && !staff.getStfLevel().isEmpty()) {
            wrapper.eq("stf_level", staff.getStfLevel());
        }
        List<Staff> staff1 = this.staffDao.selectList(wrapper);

        return staff1;
    }

    /**
     * 根据userId查询当前部门的所有员工
     *
     * @param userId
     * @return
     */
    @Override
    public List<Staff> queryByUserId(String userId) {
        Staff staff = this.queryById(userId);
        String dpNo = staff.getDpNo();
        QueryWrapper<Staff> wrapper = new QueryWrapper<>();
        wrapper.eq("dp_no", dpNo);
        //只查询除了主管以外的员工
        wrapper.in("stf_level", Arrays.asList("0", "2", "3"));
        List<Staff> staff1 = this.staffDao.selectList(wrapper);
        return staff1;
    }

    /**
     * 根据userId查询当前部门的所有在职员工
     *主要用于员工分配
     * @param userId
     * @return
     */
    @Override
    public List<Staff> queryByUserId2(String userId) {
        Staff staff = this.queryById(userId);
        String dpNo = staff.getDpNo();
        QueryWrapper<Staff> wrapper = new QueryWrapper<>();
        wrapper.eq("dp_no", dpNo);
        wrapper.eq("stf_status", "1");
        //只查询除了主管以外的员工
        wrapper.in("stf_level", Arrays.asList("0", "2", "3"));
        List<Staff> staff1 = this.staffDao.selectList(wrapper);
        return staff1;
    }


    /**
     * 新增数据
     *
     * @param staff 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(Staff staff) {
        staff.setStfNo("S" + new Random().nextInt(100000));
        staff.setStfLevel("3");
        int insert = this.staffDao.insert(staff);
        User user = new User();
        user.setUserName(staff.getStfName());
        user.setUserId(staff.getStfNo());
        String ciphertext = SaSecureUtil.aesEncrypt(passwordKey, "123456");
        user.setUserPassword(ciphertext);
        user.setRegisterTime(DateFormat.getNowDateFormat());
        // 获取员工的部门编号
        String dpNo = staff.getDpNo();
        // 根据部门编号设置权限
        switch (dpNo) {
            case "1" -> user.setUserRole("建模新手");
            case "2" -> user.setUserRole("渲染新手");
            case "3" -> user.setUserRole("后期新手");
            case "4" -> user.setUserRole("前台");
        }
        int insert1 = this.userDao.insert(user);
        return insert > 0 && insert1 > 0;
    }

    /**
     * 修改数据
     *
     * @param staff 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(Staff staff) {
        int update = staffDao.update(staff);
        return update > 0;
    }

    /**
     * 通过主键删除数据(逻辑删除）
     *
     * @param stfNo 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String stfNo) {
        //逻辑删除员工
        Staff staff = this.queryById(stfNo);
        // 判断状态是否已被删除
        if (staff.getStfStatus().equals("0")) {
            return false;
        }else {
            staff.setStfStatus("0");
            return this.update(staff);
        }
    }
}
